
#pragma once

#include "gamemessage.h"
#include "type.h"
#include "animation.h"

#include "cocos2d.h"
USING_NS_CC;

static const int TAG_NO_ZORDER = 100;

class Flag;
class Camp;
class Mine;
class Unit;
class Enemy;
class Soldier;
class Tower;
class Bullet;
class GameUILayer;
class FlagMenuLayer;

enum class GamePlayState {
	standby,
	play,
	win,
	lose,
	over,
};

enum class GamePlaySceneTouchPlace {
	blank,
	flag,
	camp,
	dragging_flag,
	soldier,
	hero,
	dragging_hero,
};

struct SoldierCreationInfo {
	SoldierType type;
	Flag* flag;
	float delay;

	SoldierCreationInfo (SoldierType t, Flag* f, float d) {
		type = t;
		flag = f;
		delay = d;
	}
};

class GamePlayScene : public Scene, public TargetedTouchDelegate, public KeypadDelegate, public MessageHolder {
public:
	GamePlayScene();
	virtual ~GamePlayScene();

    CREATE_FUNC(GamePlayScene);

	void addEnemy(Enemy* enemy);
	void addSoldier(Soldier* soldier);
	void addSceneNode(Node* node);
	void addFrameAnimations(FrameAnimation* anim);

	void removeNode(Node* node);
	void removeNodeWithFadeInOut(Node* node, float fadeInDuration, float delay, float fadeOutDuration); 

public:
	vector<Flag*> flags;
	vector<Mine*> mines;
	Camp* camp;
	
	list<Bullet*> bullets;
	list<Enemy*> enemies;
	list<Soldier*> soldiers;
	list<Soldier*> heroes;

	GamePlayState gameState;

protected:
	virtual bool init();
	virtual void update(float ticks);
	virtual void handleMessage();
	virtual void updateBullets(float ticks);
	virtual void updateEnemies(float ticks);
	virtual void updateSoldiers(float ticks);
	virtual void updateHeroes(float ticks);
	virtual void updateSoldierCreationList(float ticks);
	virtual void updateFrameAnimations(float ticks);
	
	virtual void addSceneEntities();
	virtual void addTowers();
	virtual void addMines();

	virtual void createSoldier(SoldierType type, Flag* flag);

	void showFlagMenuLayer(int flagIndex);
	
	Flag* getTouchedFlag(const Point& pt);
	Soldier* getTouchedHero(const Point& pt);
	Soldier* getTouchedSoldier(const Point& pt);

	void moveSoldiers(Flag* flag);
	void moveHero(Soldier* hero, const Point& to);

	virtual bool ccTouchBegan( Touch *pTouch, Event *pEvent );
	virtual void ccTouchMoved( Touch *pTouch, Event *pEvent );
	virtual void ccTouchEnded( Touch *pTouch, Event *pEvent );
	virtual void ccTouchCancelled( Touch *pTouch, Event *pEvent );

	virtual void keyBackClicked();
	virtual void keyMenuClicked();

	virtual void onEnter();
	virtual void onExit();

	void reorderNodes();
	void setSoldierRangeCircleVisibility(Flag* flag, bool visibility);


protected:
	Layer* _bgLayer;
	Layer* _gameLayer;
	GameUILayer* _uiLayer;
	FlagMenuLayer* _flagMenuLayer;

	GamePlaySceneTouchPlace _touchedPlace;

	list<SoldierCreationInfo> _soldierCreationList;
	list<FrameAnimation*> _frameAnimations;

	int _golds;

	char buf[200];

	static const int MAX_SORT_LIST = 200;
	Node* _arrSortNodes[MAX_SORT_LIST];

	Flag* _selectedFlag;
	Soldier* _selectedSoldier;
	Soldier* _selectedHero;
};
